<template>
  <div class="wrapper">
    <div class="uc-main clearfix">
      <div class="uc-aside">
        <user-menu></user-menu>
      </div>
      <div class="uc-content">
        <div class="uc-panel">
          <div class="uc-bigtit">品牌详情</div>
          <div class="uc-panel-bd">
            <el-form class="col-userinfo"  ref="form" :model="form" label-width="80px">
              <el-form-item  label="品牌名称">
                <el-input style="width: 320px" class="bd" v-model="form.brandName"></el-input>
              </el-form-item>
              <h3>品牌详情</h3>
              <el-table
                :data="tableData"
                style="width: 100%">
                <el-table-column
                  label="列表名称"
                  prop="categoryName">
                </el-table-column>
                <el-table-column
                  align="right">
                  <template slot="header" slot-scope="scope">
                    <el-button
                      size="mini"
                      @click="openFrom()">新增</el-button>
                  </template>
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="danger"
                      @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>

              <el-button style="background: #ff0000; color: #ffffff; width: 320px" class="tj" type="submit" @click="modifyBrand()">修改</el-button>
            </el-form>
          </div>
        </div>
      </div>
    </div>
    <el-dialog title="添加类别" :visible.sync="dialogFormVisible">
      <el-form :model="formCategory">
        <el-form-item label="类别" :label-width="formLabelWidth">
          <el-select v-model="formCategory.categoryId" placeholder="类别">
            <el-option v-for="category in categorys" :label="category.categoryName" :value="category.categoryId" :key="category.categoryId"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveBrandCategory()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import UserMenu from './UserMenu'
import Util from '../../../common/util'
import http from '../../../base/api/http'
export default {
  name: 'AdminBrandDetailsWrapper',
  components: {
    UserMenu
  },
  data () {
    return {
      form: {
        brandId: '',
        brandName: ''
      },
      formCategory: {
        brandId: '',
        categoryId: ''
      },
      categorys: [],
      tableData: [],
      search: '',
      myHeaders: {
        Authorization: Util.getToken()
      },
      dialogFormVisible: false,
      formLabelWidth: '120px'
    }
  },
  props: {
    brandId: String
  },
  methods: {
    handleDelete (index, row) {
      this.open2(row.categoryId)
    },
    openFrom (id) {
      this.dialogFormVisible = true
    },
    getCategorys () {
      http.requestGet('/java/api/category/findAll').then((res) => {
        if (res.status === 0) {
          this.categorys = res.data
        }
      })
    },
    saveBrandCategory () {
      http.requestGet('/java/api/brand/addCategory', {
        'brandId': this.form.brandId,
        'categoryId': this.formCategory.categoryId
      }).then((res) => {
        if (res.status === 0) {
          alert('操作成功')
          this.$router.go(0)
        } else {
          alert('操作失败')
        }
      })
      this.dialogFormVisible = false
    },
    deleteCategory (id) {
      http.requestGet('/java/api/brand/deleteCategory', {
        'brandId': this.form.brandId,
        'categoryId': id
      }).then((res) => {
        if (res.status === 0) {
          alert('删除成功')
          this.$router.go(0)
        } else {
          alert('删除失败')
        }
      })
    },
    open2 (id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteCategory(id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    findBrand (id) {
      http.requestGet('/java/api/brand/getBrandById', {
        'brandId': id
      }).then((res) => {
        if (res.status === 0) {
          this.form.brandName = res.data.brandName
          this.form.brandId = res.data.brandId
          this.tableData = res.data.categoryList
        }
      })
    },
    modifyBrand () {
      http.requestPost('/java/api/brand/saveBrand', this.form).then((res) => {
        if (res.status === 0) {
          alert('操作成功')
          this.$router.go(0)
        } else {
          alert('操作失败')
        }
      })
    }
  },
  mounted () {
    this.findBrand(this.brandId)
    this.getCategorys()
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 100%;
    height: 100%;
    display: block;
  }
</style>
